package com.scr1pt.extension.model.date

/**
 * 说明: 时间差值
 *
 * 创建者: tum0r
 *
 * 时间: 2021/10/15 13:53
 */
data class TimeSpan(private var millisecond: Long) : Comparable<TimeSpan> {
    /**
     * 获取毫秒差值
     */
    fun getTotalMilliseconds(): Long = millisecond

    /**
     * 获取秒差值
     */
    fun getTotalSeconds(): Double = getTotalMilliseconds() / 1000.0

    /**
     * 获取分钟差值
     */
    fun getTotalMinutes(): Double = getTotalSeconds() / 60

    /**
     * 获取小时差值
     */
    fun getTotalHours(): Double = getTotalMinutes() / 60

    /**
     * 获取天差值
     */
    fun getTotalDays(): Double = getTotalHours() / 24

    /**
     * + 运算符重载
     *
     * @param[timeSpan] 时间差
     */
    operator fun plus(timeSpan: TimeSpan): TimeSpan = TimeSpan(this.millisecond + timeSpan.millisecond)

    /**
     * + 运算符重载
     *
     * @param[millisecond] 时间差毫秒
     */
    operator fun plus(millisecond: Long): TimeSpan = TimeSpan(this.millisecond + millisecond)

    /**
     * - 运算符重载
     *
     * @param[timeSpan] 时间差
     */
    operator fun minus(timeSpan: TimeSpan): TimeSpan = TimeSpan(this.millisecond - timeSpan.millisecond)

    /**
     * - 运算符重载
     *
     * @param[millisecond] 时间差毫秒
     */
    operator fun minus(millisecond: Long): TimeSpan = TimeSpan(this.millisecond - millisecond)

    /**
     * == 和 != 运算符重载
     *
     * @param[other] 比较的对象
     */
    override fun equals(other: Any?): Boolean = when (other) {
        is TimeSpan -> this.millisecond == other.millisecond
        is Int -> this.millisecond == other.toLong()
        is Long -> this.millisecond == other
        else -> false
    }

    /**
     * TimeSpan对象的Hash值重载
     */
    override fun hashCode(): Int = millisecond.hashCode()

    /**
     * 比较操作符重载
     *
     * @param[other] 比较的对象
     */
    override fun compareTo(other: TimeSpan): Int = this.millisecond.compareTo(other.millisecond)

    /**
     * 比较操作符重载
     *
     * @param[other] 比较的对象
     */
    operator fun compareTo(other: Long): Int = this.millisecond.compareTo(other)

    /**
     * 比较操作符重载
     *
     * @param[other] 比较的对象
     */
    operator fun compareTo(other: Int): Int = this.compareTo(other.toLong())
}